package me.zhengjie.modules.sms.impl;

import com.aliyuncs.CommonResponse;
import me.zhengjie.modules.sms.SmsService;
import me.zhengjie.modules.sms.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SmsServiceImpl implements SmsService {

    @Value("${sms.signName}")
    private String signName;

    @Value("${sms.code.verificationcode}")
    private String verificationCode;

    @Value("${sms.code.messagecode}")
    private String messageCode;


    @Override
    public Map<String, Object> sendVerificationCode(String mobile, String code) {
        Map<String, Object> resultMap = new HashMap<>();
        try {

            String sendContent = "{\"code\":\"" + code + "\"}";
            CommonResponse commonResponse = SmsUtil.sendSms("", mobile, signName, verificationCode, sendContent);
            if (commonResponse != null && commonResponse.getHttpStatus() == 200) {
                resultMap.put("code", 200);
                resultMap.put("message", "发送成功");
            } else {
                resultMap.put("code", 400);
                resultMap.put("message", "发送失败");
            }
        } catch (Exception e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> sendNotify(String mobile, String title, String time, String address, String type) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            String sendContent = "{\"title\":\"" + title + "\",\"time\":\"" + time + "\",\"address\":\"" + address + "\",\"type\":" + type + "\"}";
            CommonResponse commonResponse = SmsUtil.sendSms("", mobile, signName, messageCode, sendContent);
            if (commonResponse != null && commonResponse.getHttpStatus() == 200) {
                resultMap.put("code", 200);
                resultMap.put("message", "发送成功");
            } else {
                resultMap.put("code", 400);
                resultMap.put("message", "发送失败");
            }
        } catch (Exception e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> sendBatchNotify(List<String> mobile, String title, String time, String address, String type) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            StringBuilder sendContent = new StringBuilder("[");
            for (int i = 0; i < mobile.size(); i++) {
                if (i == mobile.size() - 1)
                    sendContent.append("{\"title\":\"" + title + "\",\"time\":\"" + time + "\",\"address\":\"" + address + "\",\"type\":\"" + type + "\"}");
                else
                    sendContent.append("{\"title\":\"" + title + "\",\"time\":\"" + time + "\",\"address\":\"" + address + "\",\"type\":\"" + type + "\"},");
            }
            sendContent.append("]");
            CommonResponse commonResponse = SmsUtil.sendBatchSms(mobile, signName, messageCode, sendContent.toString());
            if (commonResponse != null && commonResponse.getHttpStatus() == 200) {
                resultMap.put("code", 200);
                resultMap.put("message", "发送成功");
            } else {
                resultMap.put("code", 400);
                resultMap.put("message", "发送失败");
            }
        } catch (Exception e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }
}
